package com.yayo.mangement.utils;

public class StringUtils {
    /**
     * 清除字符串下划线
     *
     * @param strKey
     * @return
     */
    public static String getNoUnderlineStr(String strKey) {
        if (strKey.indexOf("_") != -1) {
            String[] keyArray = strKey.split("_");
            StringBuffer sb = new StringBuffer();
            boolean flag = false;
            for (String key : keyArray) {
                if (flag) {
                    //下划线后的首字母大写
                    sb.append(org.springframework.util.StringUtils.capitalize(key));
                } else {
                    flag = true;
                    sb.append(key);
                }
            }
            strKey = sb.toString();
        }
        return strKey;
    }

    /**
     * 字符串首字母大写
     *
     * @param strKey
     * @return
     */
    public static String firstCharUpperCase(String strKey) {
        if (org.springframework.util.StringUtils.isEmpty(strKey)) {
            throw new RuntimeException("key is empty");
        }
        char[] chars = strKey.toCharArray();
        if (chars[0] >= 'a' && chars[0] <= 'z') {
            chars[0] = (char) (chars[0] - 32);
        }
        return new String(chars);
    }

    /**
     * 去掉字符串指定的前缀
     *
     * @param str    字符串名称
     * @param prefix 前缀数组
     * @return
     */
    public static String removePrefix(String str, String[] prefix) {
        if (org.springframework.util.StringUtils.isEmpty(str)) {
            return "";
        } else {
            if (null != prefix) {
                String[] prefixArray = prefix;

                for (int i = 0; i < prefix.length; ++i) {
                    String pf = prefixArray[i];
                    if (str.toLowerCase().matches("^" + pf.toLowerCase() + ".*")) {
                        return str.substring(pf.length());
                    }
                }
            }

            return str;
        }
    }

    /**
     * 判空
     *
     * @param cs
     * @return
     */
    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }
            return true;
        } else {
            return true;
        }
    }

    /**
     * 判非空
     *
     * @param cs
     * @return
     */
    public static boolean isNotBlank(CharSequence cs) {
        return !isBlank(cs);
    }


    /**
     * 将html特殊符号转换，防XSS攻击
     * @param html
     * @return
     */
    public static String htmlEncod(String html) {
        if (isBlank(html)) {
            return html;
        }
        char[] charArray = html.toCharArray();
        String newHtml = "";
        for (char c : charArray) {
            switch (c) {
                case '&':
                    newHtml += "&amp;";
                case '<':
                    newHtml += "&lt;";
                case '>':
                    newHtml += "&gt;";
                case '"':
                    newHtml += "&quot;";
                case ' ':
                    newHtml += "&nbsp;";
                default:
                    newHtml += c + "";
            }
        }
        return newHtml;
    }

}
